package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.home.dao.SpaceDrawingMapper;
import com.eastfair.home.dto.SpaceDrawingDTO;
import com.eastfair.home.entity.ReportVenueDrawing;
import com.eastfair.home.entity.ReportVenueSpace;
import com.eastfair.home.entity.SpaceDrawing;
import com.eastfair.home.service.ReportVenueDrawingService;
import com.eastfair.home.service.SpaceDrawingService;
import com.eastfair.home.vo.SpaceDrawingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 展厅图纸
 * </p>
 *
 * @author dq
 * @date 2022-09-19
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class SpaceDrawingServiceImpl extends SuperServiceImpl<SpaceDrawingMapper, SpaceDrawing> implements SpaceDrawingService {

    @Resource
    private ReportVenueDrawingService reportVenueDrawingService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<SpaceDrawing> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(SpaceDrawing model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<SpaceDrawingVO> listSpaceDrawingVOs(SpaceDrawingDTO spaceDrawingDTO) {
        log.info("listSpaceDrawingVOs - 查询展厅图纸VO列表, spaceDrawingDTO={}", spaceDrawingDTO);
        List<SpaceDrawing> list = listSpaceDrawings(spaceDrawingDTO);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list
                .stream()
                .map(spaceDrawing -> BeanUtil.toBean(spaceDrawing, SpaceDrawingVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<SpaceDrawing> listSpaceDrawings(SpaceDrawingDTO spaceDrawingDTO) {
        log.info("listSpaceDrawings - 查询展厅图纸列表, spaceDrawingDTO={}", spaceDrawingDTO);
        QueryWrapper<SpaceDrawing> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(SpaceDrawing::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(StrUtil.isNotBlank(spaceDrawingDTO.getSpaceCode()), SpaceDrawing::getSpaceCode, spaceDrawingDTO.getSpaceCode())
                .eq(spaceDrawingDTO.getDrawingCategoryId() != null, SpaceDrawing::getDrawingCategoryId, spaceDrawingDTO.getDrawingCategoryId())
                .in(spaceDrawingDTO.getDrawingCategoryIdList() != null && !spaceDrawingDTO.getDrawingCategoryIdList().isEmpty(),
                        SpaceDrawing::getDrawingCategoryId, spaceDrawingDTO.getDrawingCategoryIdList())
        ;
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveSpaceDrawing(SpaceDrawingDTO spaceDrawingDTO) {
        log.info("saveSpaceDrawing - 保存展厅图纸, spaceDrawingDTO={}", spaceDrawingDTO);
        SpaceDrawing spaceDrawing = BeanUtil.toBean(spaceDrawingDTO, SpaceDrawing.class);
        return save(spaceDrawing);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchSpaceDrawing(List<SpaceDrawingDTO> list) {
        log.info("saveBatchSpaceDrawing - 批量保存展厅图纸, list={}", list);
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(this::saveSpaceDrawing);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveByReportVenueSpace(ReportVenueSpace reportVenueSpace) {
        // 查询一键报馆的图纸
        List<ReportVenueDrawing> reportVenueDrawings = reportVenueDrawingService.listByReportVenueSpaceNumber(reportVenueSpace.getReportVenueSpaceNumber());
        if (reportVenueDrawings == null || reportVenueDrawings.isEmpty()) {
            return false;
        }
        // 图纸分类ID列表
        List<Long> drawingCategoryIds = reportVenueDrawings
                .stream()
                .map(ReportVenueDrawing::getDrawingCategoryId)
                .collect(Collectors.toList());
        String spaceCode = reportVenueSpace.getSpaceCode();
        String spaceName = reportVenueSpace.getSpaceName();
        // 删除展厅下对应图纸分类的图纸
        removeBatchBySpaceCode(spaceCode, drawingCategoryIds);
        // 批量保存展厅图纸
        List<SpaceDrawingDTO> spaceDrawingDTOList = reportVenueDrawings
                .stream()
                .map(reportVenueDrawing -> {
                    SpaceDrawingDTO spaceDrawingDTO = new SpaceDrawingDTO();
                    spaceDrawingDTO.setSpaceCode(spaceCode);
                    spaceDrawingDTO.setSpaceName(spaceName);
                    spaceDrawingDTO.setDrawingCategoryId(reportVenueDrawing.getDrawingCategoryId());
                    spaceDrawingDTO.setDrawingCategoryName(reportVenueDrawing.getDrawingCategoryName());
                    spaceDrawingDTO.setImageUrl(reportVenueDrawing.getImageUrl());
                    return spaceDrawingDTO;
                })
                .collect(Collectors.toList());
        return saveBatchSpaceDrawing(spaceDrawingDTOList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeBatchBySpaceCode(String spaceCode, List<Long> drawingCategoryIds) {
        log.info("removeBatchBySpaceCode - 展厅编码删除展厅图纸, spaceCode={}, drawingCategoryIds={}", spaceCode, drawingCategoryIds);
        SpaceDrawingDTO spaceDrawingDTO = new SpaceDrawingDTO();
        spaceDrawingDTO.setSpaceCode(spaceCode);
        spaceDrawingDTO.setDrawingCategoryIdList(drawingCategoryIds);
        List<SpaceDrawing> list = listSpaceDrawings(spaceDrawingDTO);
        if (list == null || list.isEmpty()) {
            return false;
        }
        return removeByIdsOfLogic(list);
    }
}
